<?php
/**
 * TinyCatPHP 极简、快速、创新、易用、高性能的PHP应用开发框架
 * User: CnTaiGe <hst042@163.com>
 * Date: 2017/11/19
 */

namespace TinyCatPHP\libs\handler;

use TinyCatPHP\libs\utils\Log;

class Request
{
    protected static $ip = null;
    protected static $url = null;
    protected static $uris = null;
    protected static $pathsInfo = [];

    /**
     * 获取ip地址
     * @return array|null|string
     */
    public static function getIp()
    {
        if (self::$ip !== null) return self::$ip;
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
            $pos = array_search('unknown', $arr);
            if (false !== $pos) unset($arr[$pos]);
            self::$ip = trim($arr[0]);
        } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
            self::$ip = $_SERVER['HTTP_CLIENT_IP'];
        } elseif (isset($_SERVER['REMOTE_ADDR'])) {
            self::$ip = $_SERVER['REMOTE_ADDR'];
        }
        self::$ip = ip2long(self::$ip) ? self::$ip : '0.0.0.0';
        return self::$ip;
    }

    /**
     * 请求路由
     * @return string
     */
    public static function requestUrl()
    {
        if (self::$uris === null)
            self::$uris = explode('?', $_SERVER['REQUEST_URI']);
        if (self::$url === null)
            self::$url = str_replace('/index.php', '', self::$uris[0]);
        return self::$url;
    }

    /**
     * 获取get请求内容
     * @param string $key
     * @return null
     */
    public static function requestGet($key = '')
    {
        if (isset($_GET[$key])) {
            paramFilter($_GET[$key]);
            return $_GET[$key];
        }
        return null;
    }

    /**
     * 获取post内容
     * @param $key
     * @return null
     */
    public static function requestPost($key)
    {
        if (isset($_POST[$key])) {
            paramFilter($_POST[$key]);
            return $_POST[$key];
        }
        return null;
    }

    /**
     * 获取post内容
     * @return bool|string
     */
    public static function postBody()
    {
        return file_get_contents('php://input');
    }

    /**
     * 获取post过的json数据
     * @return array|mixed
     */
    public static function jsonData()
    {
        if (strpos($_SERVER['CONTENT_TYPE'], 'application/json') !== false) {
            return json_decode(self::postBody(), true);
        }
        return [];
    }

    /**
     * 设置当前路径请求信息
     * @param $pathsInfo
     */
    public static function setPathsInfo($pathsInfo)
    {
        self::$pathsInfo = $pathsInfo;
    }

    /**
     * 获取请求的路径信息
     * @return array
     */
    public static function pathsInfo()
    {
        return self::$pathsInfo;
    }

    /**
     * 记录相关日志信息
     */
    public static function record()
    {
        Log::record('info', '请求路由: ' . self::requestUrl());
        Log::record('info', '客户端ip: ' . self::getIp());
        Log::record('info', 'user_agent: ' . $_SERVER['HTTP_USER_AGENT']);
        Log::record('info', 'GET请求内容: ' . http_build_query($_GET));
        Log::record('info', 'POST请求内容: ' . http_build_query($_POST));
        Log::record('info', 'POST_BODY: ' . self::postBody());
    }
}